import requests
from urllib.parse import urlencode


class Client:
    def __init__(self, app_id, app_secret, base_url):
        self.app_id = app_id
        self.app_secret = app_secret
        self.base_url = base_url

    def new_client(app_id, app_secret, base_url):
        return Client(app_id, app_secret, base_url)

    def post(self, endpoint, post_params):
        response = requests.post(f"{self.base_url}{endpoint}", json=post_params)
        if response.status_code != 200:
            raise Exception("接口返回错误,"+response.json().get("message"))
        resf = response.json()
        if resf.get("code") != 1:
            raise Exception("接口返回错误:"+resf.get("message"))
        return resf
    
    def get(self, endpoint, get_params):
        response = requests.get(f"{self.base_url}{endpoint}", params=get_params)
        if response.status_code != 200:
            raise Exception("接口返回错误-"+response.json().get("message"))
        resf = response.json()
        if resf.get("code") != 1:
            raise Exception("接口返回错误--"+resf.get("message"))
        return resf